// pages/particulars/particulars.js
import Toast from '../../miniprogram/@vant/weapp/dist/toast/toast'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    item: {},
    show: false,
    checked: false,
    color: 'S',
    num: 1,
    shoplgh: 0,

  },
  gobuy() {
    let arrs = []
    arrs.push({
      price: this.data.item.deposit,
      title: this.data.item.title,
      yardage: this.data.color,
      num: this.data.num,
      imgs: this.data.item.imgs
    })
    wx.setStorageSync('ZX_BUY', arrs)
    wx.navigateTo({
      url: '../buy/buy',
    })
  },

  goshopcar() {
    wx.navigateTo({
      url: '../../pages/shopcar/shopcar',
    })
  },
  addshopping() {
    wx: wx.showToast({
      title: '加入购物车成功',
    })
    let isAdd = false;
    let isAddTwo = false;
    let arr = wx.getStorageSync('ZX_SHOP') || []
    for(let i=0;i<arr.length;i++){
      if (arr[i].title == this.data.item.title) {
        isAdd = true
        if (arr[i].yardage == this.data.color) {
          arr[i].num += this.data.num
          break;
        } else {
          isAddTwo = true
        }
      }
    }
    if (!isAdd || isAddTwo) {
      arr.push({
        id:this.data.item.id,
        price: this.data.item.deposit,
        title: this.data.item.title,
        yardage: this.data.color,
        num: this.data.num,
        imgs: this.data.item.imgs
      })
    }
    wx.setStorageSync('ZX_SHOP', arr)
    this.getshoplgh()
  },

  shadecolor(color) {
    console.log(color.target.dataset.colors)
    this.setData({
      color: color.target.dataset.colors
    })
  },

  onChanges(event) {
    this.setData({
      checked: event.detail,
    });
  },

  shopping() {
    if (this.data.checked) {
      console.log('yes')
    } else {
      Toast('阅读租赁协议');
    }


  },

  onChange(event) {
    this.setData({
      num: event.detail
    })
    console.log(event.detail);

  },

  onshowss() {
    console.log(1)
  },

  showPopup() {
    this.setData({
      show: true
    });
  },

  onClose() {
    this.setData({
      show: false
    });
  },

  getItem(uid) {
    wx.request({
      url: 'http://localhost:8033/goods/getitem',
      data: {
        uid: uid
      },
      success: res => {
        let arr = {
          ...res.data.data[0]
        }
        if (arr.details) {
          arr.details = JSON.parse(arr.details)
        }
        this.setData({
          item: arr
        })
        console.log(this.data.item)
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getItem(options.uid)
  },
  getshoplgh() {
    let arr = wx.getStorageSync('ZX_SHOP')
    this.setData({
      shoplgh: arr.length
    })
    console.log(this.data.shoplgh)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getshoplgh()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (options) {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})